package uk.co.vertigomoon.pchest;

import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * Example test plugin for the BukkitCraft Server, listens to player login events and sends the player a message.
 */
public class Launcher extends JavaPlugin {
   /**
    * {@link Listener} for all {@link Event}s this plugin subscribes to.
    */
   private class BasicListener implements Listener {
      /**The message to show when a {@link Player} attempts to switch from {@link GameMode#SURVIVAL}**/
      private static final String CREATIVE_MODE_DISABLED_MSG = "Creative and Adventure Modes have been temporarily disabled, sorry =[";
      /**The message to show when Sian gets damaged**/
      private static final String EVIL_MESSAGE = "Ryan's awesome programming skills made Sian's evil plan backfire";
      /**Minecraft user name for Ryan**/
      private static final String RYAN = "mepsilax";
      /**Minecraft user name for Sian**/
      private static final String SIAN = "bulbasian";
      /**Debug variable, if true Sian will die when she hits Ryan in game**/
      private boolean killSian = false;

      /**
       * Method to handle the {@link PlayerJoinEvent}
       * @param event the {@link PlayerJoinEvent} to handle
       */
      @EventHandler public void onLogin(PlayerJoinEvent event){
         Player player = event.getPlayer();
         player.sendMessage("Hello " + player.getName() + ", Welcome to the VertigoMoon Minecraft Server!");
         if(player.getGameMode() != GameMode.SURVIVAL){
            player.setGameMode(GameMode.SURVIVAL);
         }//End if
      }//End method onLogin

      /**
       * Method to handle {@link EntityDamageByEntityEvent}s
       * @param event the event to handle.
       */
      @EventHandler(priority=EventPriority.HIGHEST) public void onEntityDamage(EntityDamageByEntityEvent event){
         if(killSian && event.getDamager() instanceof Player && event.getEntity() instanceof Player){
            Player damager = (Player)event.getDamager();
            Player damagee = (Player)event.getEntity();
            if(damager.getName().equalsIgnoreCase(SIAN) && damagee.getName().equalsIgnoreCase(RYAN)){
               damager.getWorld().strikeLightning(damager.getLocation());
               damager.setFireTicks(10000);
               event.setCancelled(true);
               Bukkit.broadcastMessage(EVIL_MESSAGE);
            }//End if
         }//End if
      }//End method onEntityDamage

      /**
       * Method to handle {@link PlayerGameModeChangeEvent}s and block any attempts to change from
       * {@link GameMode#SURVIVAL}.
       * @param event the event to handle.
       */
      @EventHandler(priority=EventPriority.HIGHEST) public void cancelCreativeMode(PlayerGameModeChangeEvent event){
         Player player = event.getPlayer();
         GameMode newGameMode = event.getNewGameMode();
         if(newGameMode == GameMode.CREATIVE || newGameMode == GameMode.ADVENTURE){
            event.setCancelled(true);
            player.setGameMode(GameMode.SURVIVAL);
            player.sendMessage(CREATIVE_MODE_DISABLED_MSG);
            getLogger().log(Level.INFO, player.getName() + " tried to change game mode but the request was denied");
         }//End if
      }//End method cancelCreativeMode
   }//End class PlayerLoginListener

   /**
    * {@inheritDoc}
    */
   @Override public void onEnable() {
      super.onEnable();
      EventListener listener = new EventListener(this);
      PluginManager manager = getServer().getPluginManager();
      manager.registerEvents(new BasicListener(), this);
      manager.registerEvents(listener, this);
      getCommand(EventListener.CHEST_COMMAND).setExecutor(listener);
   }//End method onEnable

   /**
    * {@inheritDoc}
    */
   @Override public void onDisable() {
      super.onDisable();
   }//End method onDisable
}//End class BasicTest
